<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>15-计算相机方向向量</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      width: 100%;
      height: 100%;
    }
  </style>
</head>

<body>
  <script type="module">
    import * as THREE from "three";
    import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
    import {
      scene,
      camera,
      renderer
    } from "../utils/three-base";
    import { Timer } from "three/addons/misc/Timer";
    import { renderArrowHelper } from "../utils/utils";
    
    // 渲染出三维辅助线
    renderArrowHelper();

    const endPoint = new THREE.Vector3(0, 0, 0);
    camera.position.set(0, 5, 10);
    camera.lookAt(endPoint);

    const direction = new THREE.Vector3();
    camera.getWorldDirection(direction);
    console.log(direction);

    /*
      手动计算
      中心位置点坐标 - 相机位置点坐标
      (0, 0, 0) - (0, 5, 10)
      (0-0, 0-5, 0-10) = (0, -5, -10)
      求每个分量的二次幂

      // 求和

      // 开根号
    */
    function vectorLength(x, y, z) {
      // x y z的二次幂
      const x_pow = Math.pow(x, 2);
      const y_pow = Math.pow(y, 2);
      const z_pow = Math.pow(z, 2);
      // 求和
      const sum = x_pow + y_pow + z_pow;
      // 开根号
      const sqrt = Math.sqrt(sum);
      // 归一化
      const result = new THREE.Vector3(x / sqrt, y / sqrt, z / sqrt);
      console.log(result, "手动计算结果");
    }
    // 每次都需要手动计算
    // vectorLength(0, 5 , -10);
    // threejs里Vector3里提供了计算方法，但是sub方法会影响原始值，所以需要先用clone方法返回新的vec3向量
    const minus = endPoint.clone().sub(camera.position);
    console.log(endPoint, "===========");
    vectorLength(minus.x, minus.y, minus.z);

    const cubeCamera = new THREE.Mesh(
      new THREE.SphereGeometry(0.2),
      new THREE.MeshBasicMaterial({
        color: 0xff1ff1,
        wireframe: true
      })
    );

    // 相机位置向量, 缩放0.8得到新的向量
    const pVector = camera.position.clone().multiplyScalar(0.8);
    cubeCamera.position.copy(pVector);
    scene.add(cubeCamera);

    const geometry = new THREE.BufferGeometry().setFromPoints([
      pVector,
      endPoint
    ]);
    const material = new THREE.LineBasicMaterial({
      color: 0xffffff
    });
    const line = new THREE.Line(geometry, material);
    scene.add(line);

    // 创建轨道控制器
    const controls = new OrbitControls(camera, renderer.domElement);

    // 方便摆放模型位置
    // controls.addEventListener("change", () => {
    //   console.log(camera.position);
    // })

    // 开启物体的阻尼动画效果 
    controls.enableDamping = true;

    

    // 监听controls的change事件
    controls.addEventListener("change", () => {
      // 交互的鼠标每次发生变化，都需要渲染一次(如果用了animate函数就不需要).
      renderer.render(scene, camera);
    });

    // 此类是 Clock 的替代品，具有不同的 API 设计和行为。目标是避免随着时间的推移 Clock 中变得明显的概念缺陷。
    const timer = new Timer();
    function animate(timestamp) {
      requestAnimationFrame(animate);
      timer.update(timestamp);
      const delta = timer.getDelta();
      controls.update();
      renderer.render(scene, camera);
    } 
    
    // 添加动画并渲染
    animate();
  </script>
</body>

</html>